package com.tianhui.consignortwofei.mvp.presenter;

import android.content.Context;

import com.fgs.common.CommonResponse;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.google.gson.reflect.TypeToken;
import com.tianhui.consignortwofei.mvp.contract.AuthContract;
import com.tianhui.consignortwofei.mvp.model.AuthenticationModel;
import com.tianhui.consignortwofei.mvp.model.enty.ConsignorInfo;
import com.xuexiang.xutil.net.JsonUtil;

import java.util.Map;

/**
 * @author fgs
 * @date 2021/4/6
 * @description
 **/
public class AuthPresenter extends AuthContract.Presenter {
    private AuthenticationModel mAuthModel;
    private Context mContext;

    public AuthPresenter(Context context) {
        mContext = context;
        mAuthModel = new AuthenticationModel();
    }

    @Override
    public void requestConsignorAuthenticationInfo(Map<String, String> map, boolean isDialog) {
        mAuthModel.consignorInfoModel(mContext, map, isDialog, true, getView().bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (null != getView()) {
                    if (commonResponse.getCode() == 20000) {
                        ConsignorInfo info = JsonUtil.fromJson(JsonUtil.toJson(commonResponse.getData()), new TypeToken<ConsignorInfo>() {
                        }.getType());
                        if (info != null) {
                            getView().showConsignorAuthenticationInfo(info);
                        } else {
                            getView().setMsg("获取信息失败");
                        }

                    } else {
                        getView().setMsg(commonResponse.getMsg());
                    }
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                if (null != getView()) {
                    getView().setMsg(e.message);
                }
            }
        });
    }


    @Override
    public void requestConsignorInfoCommit(ConsignorInfo info, int type) {
        mAuthModel.consignorAuthModel(mContext, info, true, getView().bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (null != getView()) {
                    if (commonResponse.getCode() == 20000) {
//                        getView().showCommitSuccess(type);
//                        if (type== PictureType.TYPE_FRONT){
//                            getView().processIdCardFrontCommitSuccess();
//                        }
                        getView().setMsg(commonResponse.getMsg());
                        getView().commitSuccess();
                    } else {
                        getView().setMsg(commonResponse.getMsg());
//                        getView().showCommitFailed(type);
                    }

                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                if (null != getView()) {
                    getView().setMsg(e.message);
//                    getView().showCommitFailed(type);
                }
            }
        });
    }

}
